package com.leecode.exercise;

/**
 * @Author: shubin_yao
 * @Date: 2022/04/03/18:32
 * @Description:
 */
public class StringPalin2 {

    public static void main(String[] args) {
        //String s = "babad";
        //String s = "ac";
        String s = "bb";
        StringPalin2 stringPalin2 = new StringPalin2();
        String s1 = stringPalin2.longestPalindrome(s);
        System.out.println(s1);
    }

    public String longestPalindrome(String s) {
        if (s == null || s.length() < 1) return "";
        int start = 0, end = 0;
        for (int i = 0; i < s.length(); i++) {
            int l = expandAroundCenter(s, i, i);
            int r = expandAroundCenter(s, i, i + 1);
            int len = Math.max(l, r);
            if (len > end - start) {
                start = i - (len - 1) / 2;
                end = i + len / 2;
            }
        }

        return s.substring(start, end + 1);
    }

    //从中心向两边返回回文字符串长度
    private int expandAroundCenter(String s, int left, int right) {
        int L = left, R = right;
        while (L >= 0 && R < s.length() && (s.charAt(L) == s.charAt(R))) {
            L--;
            R++;
        }
        return R - L - 1;
    }
}